<template>
 <el-row :gutter="24">
      <el-col :span="6"> 
        　
   <el-card class="box-card" >
      
  <div  class="clearfix" >
    <span  class="first"  style="font-size:20px;color:#000;text-decoration:none; cursor: pointer;"  >邮件分组</span><span class="addico"><i  class="header-icon el-icon-plus" @click="handleClickFolder()">添加</i></span>
  </div>
  <ul v-show="visible" :style="{left:150+left+'px',top:140+top+'px'}" class="contextmenu">
      <li  @click="handleClickFolderg(rightClickItem)"><i class="el-icon-edit" ></i>重命名</li>
      <!-- <li @click="handleSwitchg(rightClickItem)">{{statusx== 0 ? "启用" : "禁用"}}</li> -->
      <li @click="handleDelete(rightClickItem)"><i class="el-icon-delete"></i>删除</li>
      <!-- <li >关闭</li> -->
      <!-- <li @click="handleUpdate(rightClickItem)">编辑</li> -->
    </ul>
<div style="height:500px;overflow-y: auto;-ms-overflow-style: none;overflow: -moz-scrollbars-none;overflow-x:hidden; " class="over_y" >
<el-collapse accordion   v-model="activeNames" @change="handleChange" >
   <el-collapse-item name="0">
    <template slot="title">
     <i class="header-icon el-icon-info"></i><span  class="titleName" :class="{second:real}" :style="activeNames==0? 'position: relative;left: 50%;margin-left: -45px;color:#3cc1dd;' : 'position: relative;left: 50%;margin-left: -45px; ' "  >所有联系人</span>
    </template>
    <li   v-for="(item,index) in this.mrlxr" :key="index" :name="item.id" style="list-style:none;margin-top: 10px; margin-left:-25px; text-align: center; color:#3cc1dd;" >
      <span @click="DeHandle(item.id)" style=" cursor: pointer;">{{item.name}}--({{item.phone}})</span>
    </li >
  </el-collapse-item>
  <el-collapse-item v-for="(item,index) in list" :key="index" :name="item.id" @contextmenu.prevent.native="openMenu(item,$event)" >
    <template slot="title" >
     <i class="header-icon el-icon-info"></i><span class="titleName"  :style="activeNames==item.id?'position: relative;left: 50%;margin-left: -30px;color:#3cc1dd;' : 'position: relative;left: 50%;margin-left: -30px; ' " >{{item.name}}</span>
    </template>
     <a  >
    <li   v-for="(item,index) in item.children" :key="index" :name="item.id" style="list-style:none;margin-top: 10px; margin-left:-25px; text-align: center;" >
      <span @click="DeHandle(item.id)" style=" cursor: pointer;">{{item.name}}--({{item.phone}})</span>
    </li >
    </a>
  </el-collapse-item>
</el-collapse>
</div>
   </el-card>
      </el-col>
  <el-col :span="18">   
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="请输入名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="编号">
         <template slot-scope="scope">
            {{scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="联系名称" width="100px" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        label="邮箱" width="150px" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="联系电话" width="150px" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column
        prop="realName"
        header-align="center"
        align="center"
        label="真实姓名">
      </el-table-column>
      <el-table-column
        prop="sex"
        header-align="center"
        align="center"
        label="性别">
          <template slot-scope="scope">
             {{scope.row.sex == 1 ? "男" : "女"}}
        </template>
      </el-table-column>
            <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
         <template slot-scope="scope">
               <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                :active-value="1"
                :inactive-value="0"
                v-bind:title='scope.row.status == 0 ? "启用" : "禁用"'
                @change="handleSwitch(scope.row)">
        </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
           
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 8, 10, 15]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
     <detils v-if="addOrUpdateVisible" ref="addOrUpdate1" @refreshDataList="getDataList"></detils>
     <group v-if="addOrUpdateVisible" ref="addGroup" @refreshDataList="getDataList"></group>
  </div>
  </el-col>
  </el-row>
</template>

<script>

import { component as VueContextMenu } from '@xunlei/vue-context-menu'
  import AddOrUpdate from './emsgcontact-add-or-update'
  import Detils from './emsgcontact-detils'
  import Group from "./emsggroup-add-or-update"
  export default {
    watch: {
    visible(value) {
      console.log(value)
      if (value) {
        document.body.addEventListener('click', this.closeMenu)
      } else {
        document.body.removeEventListener('click', this.closeMenu)
      }
    }
  },
    data () {
      return {
      visible: false,
			top: 0,
      left: 0,
      statusx:'',
        dataForm: {
          key: ''
        }, 
        value:'100',
        activeNames: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        list:[],// 存放分组
        mrlxr:[],//默认联系人
        real:true
      }
    },
    components: {
      AddOrUpdate,
      Detils,
      'vue-context-menu': VueContextMenu,
      Group
    },
    activated () {
      this.getDataList()
      
    },
    methods: {
 openMenu(e, item) {
      this.rightClickItem = e;
      this.statusx=e.status
      var x = e.pageX;
      var y = e.pageY;

      this.top = y;
      this.left = x;
      
      this.visible = true;
    },
    closeMenu() {
      this.visible = false;
    },
      // 获取数据列表
      getDataList () {
        this.getgroup()
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/emsgcontact/list'),
          method: 'get',
          params: this.$http.adornParams({
            'pageNumber': this.pageIndex,
            'pageSize': this.pageSize,
            'key': this.dataForm.key,
            id:sessionStorage.getItem("group_id")
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.mrlxr=data.page.records
            this.totalPage = data.page.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      //显示所有
      getShowall(){
        sessionStorage.setItem("group_id","")
        this.real=true
        this.getDataList()
      },
       handleClickFolder(id){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addGroup.init(id)
        })
      },
      handleClickFolderg(val){
        let id=val.id
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addGroup.init(id)
        })
      },
      handleDelete(val){
        let id=val.id
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        console.log(ids)
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/emsggroup/delete/${ids}`),
            method: 'get',
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //获取组
      getgroup () {
        this.$http({
          url: this.$http.adornUrl('/emsggroup/group'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.list = data.list
          } else {
          }
        })
      },
      //分组禁用启用状态
       handleSwitchg(val){
         this.$http({
          url: this.$http.adornUrl(`/emsggroup/status/${val.id}/${val.status}`),
          method: 'get',
          data: this.$http.adornParams({

          })
        }).then(({data}) => {
          if (data && data.code === 0) {
               this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
          } else {
          }
        })
      },
      //禁用启用
      handleSwitch(val){
        this.$confirm(`确定对[id=${val.id}]进行[${val.status==1 ? '启用' : '禁用'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         this.$http({
          url: this.$http.adornUrl(`/emsgcontact/status/${val.id}/${val.status}`),
          method: 'get',
          data: this.$http.adornParams({

          })
        }).then(({data}) => {
          if (data && data.code === 0) {
               this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                 
                })
          } else {
          }
        })
        })
      },
      //获取组id
       handleChange(val) {
         console.log(val)
         if(val==0){
          this.$http({
          url: this.$http.adornUrl('/emsgcontact/list'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.mrlxr=data.page.records
             this.dataList = data.page.records
              sessionStorage.setItem("group_id", "");
          }
        })
         }
        else if(val!=''){
          this.real=false
          sessionStorage.setItem("group_id", val);
          this.$http({
          url: this.$http.adornUrl('/emsgcontact/groupById'),
          method: 'post',
          data: this.$http.adornParams({
            id:val
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.list
          } else {
          }
        })
        }else{
           sessionStorage.setItem("group_id", "");
        }
      
       
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      DeHandle(id){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate1.init(id)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/emsgcontact/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style scoped>
li{
  list-style:none;
  margin-left: 50px;
  margin-top: 10px;
}
.titleName{
position: relative;
left: 50%;
 margin-left: -30px;
}
.clearfix{
  text-align: center;
}
a{
  text-decoration:none;
}
.second{
  color:#3cc1dd !important;
}
.over_y::-webkit-scrollbar {display:none}
.contextmenu {

  background: #fff;
  z-index: 3000;
  position: absolute;
  list-style-type: none;
  padding: 5px 0;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  color: #333;
  box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
}
.contextmenu ul{
  position: absolute;
  top: 50% !important;
  left: 50% !important;
}
.contextmenu li {
  margin: 0;
  padding: 7px 16px;
  margin-top: 15px;
  cursor: pointer;
}

.contextmenu li:hover {
  background: #eee;
}
.addico{
  position: absolute;
  left:20%;
  padding: 3px 0; cursor: pointer;
  }

</style>
